package com.moyun.zbmy.zizhou.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.moyun.zbmy.zizhou.R;
import com.moyun.zbmy.zizhou.beans.AccountTagBean;
import com.moyun.zbmy.zizhou.ui.adapter.holder.AccountTypeHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 公众号类型列表
 */
public class AccountTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<AccountTagBean> dataList = new ArrayList<>();
    private OnTypeClickListener listener;

    public AccountTypeAdapter(Context context, OnTypeClickListener listener) {
        this.context = context;
        this.listener = listener;
    }


    @Override
    public AccountTypeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_account_type, parent, false);
        AccountTypeHolder holder = new AccountTypeHolder(view);
        return holder;
    }

    public void setData(List<AccountTagBean> list) {
        if (null != list) {
            dataList = list;
        }
        notifyDataSetChanged();
    }

    public void addData(List<AccountTagBean> list) {
        int pos = getItemCount();
        if (null != list) {
            dataList.addAll(list);
        }
        if (pos > 0) {
            notifyItemRangeChanged(pos, getItemCount());
        }
    }

    public void updateData(List<AccountTagBean> list) {
        if (null != list) {
            dataList = list;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return 0;//dataList.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final AccountTagBean info = dataList.get(position);
        final AccountTypeHolder viewHolder = (AccountTypeHolder) holder;


        viewHolder.title.setText(info.getNodeName());
        viewHolder.item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.OnTypeClick(position);
                }
            }
        });
        if (info.isSelected()) {
            viewHolder.title.setTextColor(context.getResources().getColor(R.color.colorHomeText));
        } else {
            viewHolder.title.setTextColor(context.getResources().getColor(R.color.color_666666));
        }

    }


    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public interface OnTypeClickListener {
        void OnTypeClick(int position);
    }
}
